//
//  NewEditorWord.m
//  shusheng
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "NewEditorWord.h"

#define  PATH_FILE [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]

@implementation NewEditorWord


+ (void)creatOneTxtFile:(NSString *)fileName{

//    NSFileManager *fileManage = [NSFileManager defaultManager];
//    [fileManage changeCurrentDirectoryPath:[PATH_FILE stringByExpandingTildeInPath]];//文件目录
//     NSString *path = [PATH_FILE stringByAppendingString:fileName];//路径
//    
////     [fileManage removeItemAtPath:fileName error:nil];//创建文件
////     NSString *path = [PATH_FILE stringByAppendingString:fileName];//路径
////    NSMutableData *writer = [[NSMutableData alloc]init];//数据缓冲
//////    [writer appendData:[file]]
////    
//    
//    //
//    [fileManage createFileAtPath:path contents:[@"" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//    NSLog(@"txt文件路径%@",path);
    
//    //去掉txt
//    NSString *name = [[fileName lastPathComponent]stringByDeletingPathExtension];
//    //给文件取个文件名
//    NSString * fileNameOfTxt = [PATH_FILE stringByAppendingPathComponent:name];
//    NSLog(@"文件名,路径%@",fileNameOfTxt);
//    BOOL blhave = [[NSFileManager defaultManager]fileExistsAtPath:fileNameOfTxt];
//    if (blhave) {
//        NSLog(@"已存在txt");
//        return;
//    }
//
//    NSString *strPath = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
//    NSData *data = [NSData dataWithContentsOfFile:strPath];
//    BOOL result  = [data writeToFile:fileNameOfTxt atomically:YES];
//    if (result) {
//        NSLog(@"txt创建成功");
//    }else{
//        NSLog(@"txt创建失败");
//    }
    
    NSString *filePath= [PATH_FILE stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [[NSFileManager alloc]init];
    NSString *fileContent = @"";
    NSData *fileData = [fileContent dataUsingEncoding:NSUTF8StringEncoding];
    [fileManager createFileAtPath:filePath contents:fileData attributes:nil];
    NSLog(@"txt文件创建路径%@",filePath);
    
}



+ (NSString *)readFileName:(NSString*)name{
 
//    //读取数据：
    NSString*iOSPath = [PATH_FILE stringByAppendingPathComponent:name];
    NSLog(@"读取数据~txt文件路径%@",iOSPath);

    NSString*content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
 
    return content;
}

+ (void)writeToOneTxt:(NSString *)txtfileName
                     words:(NSString *)words{
    
    //获取文件路径
    NSString *path = [PATH_FILE stringByAppendingPathComponent:txtfileName];

    //创建数据缓冲
    NSMutableData *writer = [[NSMutableData alloc] init];
    //将字符串添加到缓冲中
    [writer appendData:[words dataUsingEncoding:NSUTF8StringEncoding]];

    //将缓冲的数据写入到文件中
    [writer writeToFile:path atomically:YES];
   
}


+ (void)deleteEditor:(NSString *)txtfileName{
//    NSFileManager *fileManage = [NSFileManager defaultManager];
//    //获取文件路径
//    NSString *path = [PATH_FILE stringByAppendingPathComponent:txtfileName];
//    NSLog(@"txt文件路径%@",path);
//    BOOL isHaveFile =  [fileManage fileExistsAtPath:path];
//    if (isHaveFile) {
//        //删
//    
//        BOOL isDelet = [fileManage removeItemAtPath:path error:nil];
//        if (isDelet) {
//            NSLog(@"删除成功");
//        }else{
//            NSLog(@"删除失败");
//        }
//        
//    }else{
//        NSLog(@"没有该文件");
//        return;
//    }
    
    
     NSFileManager *fileManage = [NSFileManager defaultManager];
     NSString *fileNameOfTxt = [PATH_FILE stringByAppendingPathComponent:txtfileName];
    NSLog(@"删除时路径%@",fileNameOfTxt);
    BOOL blhave = [[NSFileManager defaultManager] fileExistsAtPath:fileNameOfTxt];
    if (!blhave) {
        NSLog(@"文件不存在");
        return;
    }else{
        NSLog(@"文件存在");
        BOOL blDele = [fileManage removeItemAtPath:fileNameOfTxt error:nil];
        if (blDele) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }
    
    
}
@end
